package top.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import top.api.common.ResUlt;
import top.api.domain.dto.PageDto;
import top.api.domain.dto.UserAddDto;
import top.api.domain.dto.UserEditDto;
import top.api.domain.entity.User;
import top.api.domain.vo.PageVo;
import top.api.enums.CommonStatusEnum;
import top.api.service.UserService;
import top.api.mapper.UserMapper;
import org.springframework.stereotype.Service;
import top.api.utils.AssertUtil;
import top.api.utils.BeanCopyUtils;

import java.util.List;

/**
* @author 27465
* @description 针对表【user】的数据库操作Service实现
* @createDate 2023-12-13 14:27:32
*/
@Service
@RequiredArgsConstructor
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Override
    public ResUlt<?> pageList(PageDto pageDto, Integer id, String username) {
        Page<User> pageInfo = new Page<>(pageDto.getPageNum(),pageDto.getPageSize());

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectUtils.isNotNull(id),User::getId,id);
        queryWrapper.eq(StringUtils.isNotBlank(username),User::getUsername,username);

        baseMapper.selectPage(pageInfo,queryWrapper);

        PageVo<User> pageVo = new PageVo<>(pageInfo.getRecords(),pageInfo.getTotal());

        return ResUlt.success(pageVo);
    }

    @Override
    public ResUlt<?> getUserInfo(Integer id) {
        return ResUlt.success(baseMapper.selectById(id));
    }

    @Override
    public ResUlt<?> addUser(UserAddDto userAddDto) {
        User user = BeanCopyUtils.copyBean(userAddDto, User.class);
        user.setStatus(CommonStatusEnum.success);

        // 判断用户名是否存在
        Long count = baseMapper.selectCount(Wrappers.<User>lambdaQuery()
                .eq(User::getUsername, userAddDto.getUsername()));
        AssertUtil.isTrue(count > 0,"用户名已存在");

        int flag = baseMapper.insert(user);

        return ResUlt.successOrError(flag);
    }

    @Override
    public ResUlt<?> editUser(UserEditDto userEditDto) {
        User user = BeanCopyUtils.copyBean(userEditDto, User.class);

        // 判断用户名是否存在 不包括自己
        Long count = baseMapper.selectCount(Wrappers.<User>lambdaQuery()
                .eq(User::getUsername, userEditDto.getUsername())
                .ne(User::getId,userEditDto.getId()));
        AssertUtil.isTrue(count > 0,"用户名已存在");

        int flag = baseMapper.updateById(user);

        return ResUlt.successOrError(flag);
    }

    @Override
    public ResUlt<?> delUser(List<Integer> ids) {
        // 不允许删除超级用户管理员
        AssertUtil.isTrue(ids.contains(1),"不允许删除超级用户管理员");

        int count = baseMapper.deleteBatchIds(ids);
        return ResUlt.success("删除成功: ["+count+"]");
    }
}




